# Table 2
# libs --------------------------------------------------------------------
library(here)
library(tidyverse)
library(haven)
library(lfe)
library(stargazer)
library(knitr)
library(xtable)

# data --------------------------------------------------------------------
data <- read_dta(file = '../data/modified data/analysis.dta')

# make table 2 col 1
m1 <- felm(share_votes_dem ~ treat | county_id + state_year_id | 0 | county_id, data = data)
b1 <- round(m1$coefficients[1], 3)
se1 <- round(m1$cse[1], 3)
n1 <- m1$N
nc1 <- length(levels(m1$fe$county_id))
ne1 <- length(levels(m1$fe$state_year_id))

# make table 2 col 2
temp <- data %>% mutate(county_id = as.factor(county_id), year = as.numeric(year))
m2 <- felm(share_votes_dem ~ treat | county_id + county_id:year + state_year_id | 0 | county_id, data = temp)
b2 <- round(m2$coefficients[1], 3)
se2 <- round(m2$cse[1], 3)
n2 <- m2$N
nc2 <- length(levels(m2$fe$county_id))
ne2 <- length(levels(m2$fe$state_year_id))

# make table 2 col 3
temp <- data %>% mutate(county_id = as.factor(county_id), year = as.numeric(year), year2 = as.numeric(year2))
m3 <- felm(share_votes_dem ~ treat | county_id:year + county_id:year2 + state_year_id | 0 | county_id, data = temp)
b3 <- round(m3$coefficients[1], 3)
se3 <- round(m3$cse[1], 3)
n3 <- m3$N
nc3 <- length(levels(m3$fe[["year:county_id"]]))
ne3 <- length(levels(m3$fe$state_year_id))

# prepare an analysis dataset with each row being a state-year-office
data_long <- data %>% dplyr::select(state, county, county_id, year, state_year_id, dem_share_gov, dem_share_pres, dem_share_sen, treat, year2, year3) %>% 
  mutate(year = as.character(year)) %>% 
  tidyr::pivot_longer(cols = starts_with("dem_share"), names_to = "office", values_to = "dem_share")

# make table 2 col 4
temp <- data_long %>% mutate(county_id = as.factor(county_id), year = as.numeric(year), year2 = as.numeric(year2))
m4 <- felm(dem_share ~ treat | county_id + state_year_id | 0 | county_id, data = temp)
b4 <- round(m4$coefficients[1], 3)
se4 <- round(m4$cse[1], 3)
n4 <- m4$N
nc4 <- length(levels(m4$fe$county_id))
ne4 <- length(levels(m4$fe$state_year_id))

# make table 2 col 5
temp <- data_long %>% mutate(county_id = as.factor(county_id), year = as.numeric(year), year2 = as.numeric(year2))
m5 <- felm(dem_share ~ treat | county_id + state_year_id + county_id:year| 0 | county_id, data = temp)
b5 <- round(m5$coefficients[1], 3)
se5 <- round(m5$cse[1], 3)
n5 <- m5$N
nc5 <- length(levels(m5$fe$county_id))
ne5 <- length(levels(m5$fe$state_year_id))

# make table 2 col 6
temp <- data_long %>% mutate(county_id = as.factor(county_id), year = as.numeric(year), year2 = as.numeric(year2), cy2int = as.numeric(county_id) * as.numeric(year2))
m6 <- felm(dem_share ~ treat | state_year_id + county_id + county_id:year + cy2int | 0 | county_id, data = temp)
b6 <- round(m6$coefficients[1], 3)
se6 <- round(m6$cse[1], 3)
n6 <- m6$N
nc6 <- length(levels(m6$fe$county_id))
ne6 <- length(levels(m6$fe$state_year_id))

# build table
tbl <- tibble(" " = c("VBM", "", "# Counties", "# Elections","# Obs", "County FE", "State by Year FE", "County Trends"),
              "(1)" = c(b1, paste0("(", se1, ")"), nc1, ne1, n1, "Yes", "Yes", "No"),
              "(2)" = c(b2, paste0("(", se2, ")"), nc2, ne2, n2, "Yes", "Yes", "Linear"),
              "(3)" = c(b3, paste0("(", se3, ")"), nc3, ne3, n3, "Yes", "Yes", "Quad"),
              "(4)" = c(b4, paste0("(", se4, ")"), nc4, ne4, n4, "Yes", "Yes", "No"),
              "(5)" = c(b5, paste0("(", se5, ")"), nc5, ne5, n5, "Yes", "Yes", "Linear"),
              "(6)" = c(b6, paste0("(", se6, ")"), nc6, ne6, n6, "Yes", "Yes", "Quad"))
x <- xtable(tbl, caption = "\\textbf{Vote-by-Mail Expansion Does Not Appear to Favor Either Party.}", label = "tab:partisan_effects")
align(x) <- c("l", "l", rep("c", 6))
print(x, file = "../repfig/partisan_effects_table.tex", compress = F, hline.after = c(-1, -1, 0, 5), hline.before = 1,
      include.rownames = F, include.colnames = F, caption.placement = "top",
      add.to.row = list(pos = list(0, 0, nrow(tbl)), command = c("& \\multicolumn{3}{c}{Dem Turnout Share [0-1]} & \\multicolumn{3}{c}{Dem Vote Share [0-1]} \\\\\n",
                                                      "& (1) & (2) & (3) & (4) & (5) & (6) \\\\\n",
                                                      "\\hline\\hline\\multicolumn{7}{p{.8\\textwidth}}{\\footnotesize{Robust standard errors clustered by county in parentheses. The number of counties is smaller in columns 1-3 because we have partisan turnout share for CA and UT, but not WA. Columns 4-6 use data from all three states.}} \\\\\n")))
